package com.cskaoyan.app.wxcontroller;

import com.cskaoyan.app.bean.bo.CommentBO;
import com.cskaoyan.app.bean.bo.OrderIDBO;
import com.cskaoyan.app.bean.bo.SubmitBO;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketOrderGoods;
import com.cskaoyan.app.bean.vo.orderVo.WeChatOrderDetailVO;
import com.cskaoyan.app.bean.vo.orderVo.WeChatOrderListVO;
import com.cskaoyan.app.wxservice.WeChatOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/31 14:42
 */
@RestController
@RequestMapping("wx/order")
public class WeChatOrderController {

    @Autowired
    WeChatOrderService weChatOrderService;

    @GetMapping("list")
    public BaseRespVo<Object> list(Integer showType,Integer page, Integer limit){
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        if(userid == null){
            return BaseRespVo.unAuthed();
        }
        System.out.println(userid+"*********************************");
        CommonData<WeChatOrderListVO> data = weChatOrderService.list(showType,page,limit,userid);
        return BaseRespVo.ok(data);
    }

    @GetMapping("detail")
    public BaseRespVo<Object> detail(Integer orderId){
        WeChatOrderDetailVO weChatOrderDetailVO = weChatOrderService.detail(orderId);
        return BaseRespVo.ok(weChatOrderDetailVO);
    }

    @PostMapping("refund")
    public BaseRespVo<Object> refund(@RequestBody @Validated OrderIDBO orderIDBO){
        Integer orderId = orderIDBO.getOrderId();
        Integer row = weChatOrderService.refund(orderId);
        if(row != 1){return BaseRespVo.invalidParameter("退款失败！");}
        return BaseRespVo.ok();
    }

    @PostMapping("confirm")
    public BaseRespVo<Object> confirm(@RequestBody @Validated OrderIDBO orderIDBO){
        Integer orderId = orderIDBO.getOrderId();
        Integer row = weChatOrderService.confirm(orderId);
        if(row != 1){return BaseRespVo.invalidParameter("收货失败！");}
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody @Validated OrderIDBO orderIDBO){
        Integer orderId = orderIDBO.getOrderId();
        Integer row = weChatOrderService.delete(orderId);
        if(row != 1){return BaseRespVo.invalidParameter("删除失败！");}
        return BaseRespVo.ok();
    }

    @GetMapping("goods")
    public BaseRespVo<Object> goods(Integer orderId, Integer goodsId){
        MarketOrderGoods marketOrderGoods = weChatOrderService.goods(orderId,goodsId);
        return BaseRespVo.ok(marketOrderGoods);
    }

    @PostMapping("comment")
    public BaseRespVo<Object> comment(@RequestBody @Validated CommentBO commentBO){
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        if(userid == null){
            return BaseRespVo.unAuthed();
        }
        Integer row = weChatOrderService.comment(commentBO,userid);
        return BaseRespVo.ok();
    }

    @PostMapping("submit")
    public BaseRespVo<Object> submit(@RequestBody @Validated SubmitBO submitBO){
        Subject subject = SecurityUtils.getSubject();
        Integer userid = (Integer) subject.getSession().getAttribute("userid");
        if(userid == null){
            return BaseRespVo.unAuthed();
        }
        Integer code = weChatOrderService.submit(submitBO,userid);
        if(code == 400){
            return BaseRespVo.invalidParameter("购买失败！");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("prepay")
    public BaseRespVo<Object> prepay(@RequestBody @Validated OrderIDBO orderIDBO){
        Integer row = weChatOrderService.prepay(orderIDBO);
        return BaseRespVo.ok();
    }

    @PostMapping("cancel")
    public BaseRespVo<Object> cancel(@RequestBody @Validated OrderIDBO orderIDBO){
        Integer row = weChatOrderService.cancel(orderIDBO);
        return BaseRespVo.ok();
    }
}
